package com.roamer.school.service.converter.sys;

import com.roamer.school.repository.entity.sys.SysUser;
import com.roamer.school.service.model.sys.UserDTO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.NullValueCheckStrategy;
import org.mapstruct.factory.Mappers;

/**
 * 用户数据转换接口
 *
 * @author roamer
 * @version V1.0
 * @date 2019/1/24 17:42
 */

@Mapper(nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS)
public interface UserConverterMapper {

    UserConverterMapper MAPPER = Mappers.getMapper(UserConverterMapper.class);


    /**
     * 转换为SysUser
     *
     * @param userDTO DTO对象
     *
     * @return {@code SysUser}
     */
    @Mapping(source = "id", target = "id")
    @Mapping(expression = "java(com.roamer.school.repository.enums.UserStatusEnum.findByValue(userDTO.getStatus()))",
             target = "status")
    SysUser reverseConvert(UserDTO userDTO);

    /**
     * 转换为UserDTO
     *
     * @param user SysUser对象
     *
     * @return {@code SysUser}
     */
    @Mapping(source = "id", target = "id")
    @Mapping(source = "status.value", target = "status")
    UserDTO convert(SysUser user);

    /**
     * 复制属性
     *
     * @param userDTO {@code UserDTO} 源对象
     * @param user    {@code SysUser} 目标对象
     */
    @Mapping(expression = "java(com.roamer.school.repository.enums.UserStatusEnum.findByValue(userDTO.getStatus()))",
             target = "status")
    void copyPropertyFromDTO(UserDTO userDTO, @MappingTarget SysUser user);

}
